package com.generalbackend.common.response;

import java.util.Objects;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

/**
 * @author tim.xie
 * @date 2021/11/29 17:21
 */
public interface ResponseBuilder<T extends Response> extends UnaryOperator<T> {
    /**
     * 初始化构建器
     */
    ResponseBuilder<Response> BUILDER = r -> r == null ? new Response() : r;
    /**
     * 请求成功
     */
    ResponseBuilder<Response> SUCCESS = BUILDER.code(0);
    ResponseBuilder<Response> LOGIN_SUCCESS = SUCCESS.message("登录成功");
    ResponseBuilder<Response> LOGOUT_SUCCESS = SUCCESS.message("登出成功");
    ResponseBuilder<Response> QUERY_SUCCESS = SUCCESS.message("请求成功");
    ResponseBuilder<Response> INSERT_SUCCESS = SUCCESS.message("新增成功");
    ResponseBuilder<Response> UPDATE_SUCCESS = SUCCESS.message("保存成功");
    ResponseBuilder<Response> DELETE_SUCCESS = SUCCESS.message("删除成功");
    /**
     * 权限不足
     */
    ResponseBuilder<Response> PERMISSION = BUILDER.code(403);
    ResponseBuilder<Response> NOT_LOGIN = PERMISSION.message("未登录");
    /**
     * 服务端异常
     */
    ResponseBuilder<Response> ERROR = BUILDER.code(500);
    ResponseBuilder<Response> QUERY_ERROR = ERROR.message("查询失败: %s");
    ResponseBuilder<Response> INSERT_ERROR = ERROR.message("新增失败: %s");
    ResponseBuilder<Response> UPDATE_ERROR = ERROR.message("保存失败: %s");
    ResponseBuilder<Response> DELETE_ERROR = ERROR.message("删除失败: %s");

    /**
     * 消息码构建器
     * @param code
     * @return
     */
    default ResponseBuilder<T> code(int code) {
        return andThen(r -> {
            r.setCode(code);
            return r;
        });
    }

    /**
     * 消息构建器
     * @param msg
     * @return
     */
    default ResponseBuilder<T> message(String msg) {
        return andThen(r -> {
            r.setMessage(msg);
            return r;
        });
    }

    /**
     * 返回数据构建器
     * @param data
     * @return
     */
    default ResponseBuilder<T> data(Object data) {
        return andThen(r -> {
            r.setData(data);
            return r;
        });
    }

    /**
     * 格式化消息信息
     * @param msgs
     * @return
     */
    default ResponseBuilder<T> msgArg(Object... msgs) {
        return andThen(r -> {
            r.setMessage(String.format(r.getMessage(), msgs));
            return r;
        });
    }

    /**
     * 构建器连接
     * @param after
     * @return
     */
    default ResponseBuilder<T> andThen(ResponseBuilder<T> after) {
        Objects.requireNonNull(after);
        return r -> after.apply(apply(r));
    }

    /**
     * 开始构建
     * @param data
     * @return
     */
    default T build(Object data) {
        T t = apply(null);
        t.setData(data);
        return t;
    }

    /**
     * 开始构建
     * @return
     */
    default T build() {
        return apply(null);
    }

    /**
     * 自定义构建器
     * @param creator
     * @param <R>
     * @return
     */
    default <R extends T> ResponseBuilder<R> create(Supplier<R> creator) {
        return r -> (R) apply((r == null ? creator.get() : r));
    }
}
